﻿using Core.Dtos;
using Core.Domain;
using Core.IRepository;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Core.ViewModels;
using Library.Cache;
using Library.Helpers;
using Library.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Core.Mappers;
using Core.Aspects;
using Core.Context;
using Core.IRepository.Admin;

namespace Services
{
    [CacheName("Setting")]
    public class ReadSettingService : BaseService, IReadSettingService
    {
        private const string CACHE_SETTINGS = "Settings";

        private readonly IAdminRepository<int, Setting> repository;
        private readonly IAdminUnitOfWork unitOfWork;        

        public ReadSettingService(IAdminUnitOfWork unitOfWork, IAdminRepository<int, Setting> repository)
        {
            this.repository = repository;
            this.unitOfWork = unitOfWork;        
        }

        private IEnumerable<SettingDto> FindAll()
        {
            return repository.FindAll().Map<Setting, SettingDto>();
        }

        [Cache(Name = CACHE_SETTINGS, NoExpiration = true)]
        public AppSettings GetAll()
        {
            AppSettings info = new AppSettings();
            var settings = FindAll();
            info.List_PageSize = int.Parse(settings.GetValue("List.PageSize"));
            info.List_LinksSize = int.Parse(settings.GetValue("List.LinksSize"));
            info.Reports_PolicyPath = settings.GetValue("Reports.PolicyPath");
            return info;
        }

        public ClientSettingsVM GetAllForClient()
        {
            return GetAll().Map<ClientSettingsVM>();
        }

        public void InitStaticCache()
        {
            GetAll();
        }

        [DeleteCache(Name = CACHE_SETTINGS)]
        public Response ResetStaticCache()
        {
            return Run(() =>
            {
                InitStaticCache();
            });
        }
    }
}